#include<iostream>
#include<cmath>
#include<vector>
#include "../programming1/EquationSolver.h"
using namespace std;


double B(const int& n, const int& i, const double& x) {
	if (n == 0) {
		if (i-1 < x && x <= i) return 1;
		else return 0;
	}
	return (x-i+1) * B(n-1, i, x) / n + (i+n-x) * B(n-1, i+1, x) / n;
}

class LinearBSpline{
private:
	int L, R;
	vector<double> coefficient;
public:
	LinearBSpline(const int& x0, const int& n, const vector<double>& a) : L(x0), coefficient(a) {
		R = L + n;
	}
	double getValue(const double& _x) {
		if (_x < L || _x > R) throw "Out of Range!";
		int i = floor(_x);
		double res = 0;
		if(i >= L && i <= R) res += coefficient[i-L] * B(1, i,  _x);
		if(i+1 >= L && i+1 <= R) res += coefficient[i+1-L] * B(1, i+1, _x);
		return res;
	}
};


LinearBSpline LinearBSplineInterpolation( Function & f, const int& l, const int& n) {
	vector <double> a(n+1);
	for(int i = 0; i <= n; ++ i) a[i] = f(l+i);
	return LinearBSpline(l, n, a);
}